const express = require('express');
const path = require('path');
const app = express();
const port = 13865;

// 首页
app.use('/home', express.static(path.join(__dirname, '../home')));
// 启动服务
async function startServer() {
  return new Promise((resolve, reject) => {
    try {
      // HTTP 服务
      const server = app.listen(port, '127.0.0.1', () => {
        console.log(`* 🚀 ${port} 后台服务启动成功.`);
      });
      server.on('error', reject);
    } catch (error) {
      reject(error);
    } finally {
      resolve();
    }
  });
}

/**
 * 开发使用功能
 */
async function development() {
  if (process.env.NODE_ENV === 'server') {
    // 启动服务(客户端服务\签名服务\代理服务)
    await startServer();
  }
}

development();

module.exports = { startServer };
